package ru.usu.gv.gui.frames.styles;

import ru.usu.gv.gui.actions.ShowEdgesAction;
import ru.usu.gv.gui.component.GVIntegerField;
import ru.usu.gv.gui.frames.ISaveCallback;
import ru.usu.gv.styles.Style;
import ru.usu.gv.styles.StyleFacade;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.List;

/**
 @author Sergey Pupyrev 
 11.04.2007
*/

public class EditStyleFrame extends JFrame
{
	private List<ISaveCallback> saveCallbacks = new ArrayList<ISaveCallback>();

	private Style style;

	public EditStyleFrame(Style style)
	{
		this.style = style;
		initComponents();
		setTitle("Редактирование стиля");
		setResizable(false);
	}

	private void initComponents()
	{
		//SETTINGS
		JPanel mainPanel = new JPanel();
		mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));

		mainPanel.add(createTitlePanel());
		mainPanel.add(new JSeparator());
		mainPanel.add(createVertexEdgePanel());
		mainPanel.add(createSystemSettings());

		getContentPane().add(mainPanel, BorderLayout.NORTH);
		getContentPane().add(createSaveCancelPanel(), BorderLayout.SOUTH);
		pack();
	}

	private JPanel createTitlePanel()
	{
		JPanel titlePanel = new JPanel(new GridLayout(2, 2, 5, 5));
		titlePanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

		JLabel label1 = new JLabel("Наименование");
		label1.setForeground(new Color(0, 0, 160));
		titlePanel.add(label1);
		JTextField styleIdField = new JTextField(style.getStringAttribute("common.id"), 20);
		styleIdField.setEditable(false);
		titlePanel.add(styleIdField);

		JLabel label2 = new JLabel("Locale");
		label2.setForeground(new Color(0, 0, 160));
		titlePanel.add(label2);
		JTextField styleLocaleField = new JTextField(StyleFacade.getLocale(style), 20);
		styleLocaleField.setEditable(false);
		titlePanel.add(styleLocaleField);

		return titlePanel;
	}

	private JPanel createVertexEdgePanel()
	{
		//VertexEdge
		JPanel vertexEdgePanel = new JPanel();
		vertexEdgePanel.setLayout(new GridLayout(1, 2));

		vertexEdgePanel.add(new VertexStylePanel(style, saveCallbacks), BorderLayout.WEST);
		vertexEdgePanel.add(new EdgeStylePanel(style, saveCallbacks), BorderLayout.EAST);

		return vertexEdgePanel;
	}

	private JPanel createSaveCancelPanel()
	{
		JPanel applyPanel = new JPanel();

		//apply
		JButton applyButton = new JButton("Сохранить");
		applyButton.addActionListener(GUIStyleUtilities.createApplyListener(this, saveCallbacks));

		//cancel
		JButton cancelButton = new JButton("Отмена");
		cancelButton.addActionListener(GUIStyleUtilities.createCancelListener(this));

		applyPanel.add(applyButton);
		applyPanel.add(cancelButton);
		return applyPanel;
	}

	private JPanel createSystemSettings()
	{
		JPanel systemPanel = new JPanel();
		systemPanel.setBorder(new TitledBorder("Системные настройки"));
		systemPanel.setLayout(new GridLayout(3, 1));

		int historySize = StyleFacade.getDefaultIntegerAttribute("history.size");
		GVIntegerField history = new GVIntegerField("Количество команд в истории", historySize, true);
		systemPanel.add(history);

		final JCheckBox multiGraphCheckBox = new JCheckBox("Ориентированный", style.getBooleanAttribute(StyleFacade.DIRECTED_GRAPH_ATTRIBUTE));
		systemPanel.add(multiGraphCheckBox);
		multiGraphCheckBox.setEnabled(false);
		saveCallbacks.add(new ISaveCallback()
		{
			public void doSave()
			{
				//TODO!!!
				style.setBooleanAttribute(StyleFacade.DIRECTED_GRAPH_ATTRIBUTE, multiGraphCheckBox.isSelected());
			}
		});

		final JCheckBox showVertexLabelsCheckBox = new JCheckBox("Показывать метки вершин", style.getBooleanAttribute(StyleFacade.SHOW_VERTEX_LABEL_ATTRIBUTE));
		systemPanel.add(showVertexLabelsCheckBox);
		saveCallbacks.add(new ISaveCallback()
		{
			public void doSave()
			{
				style.setBooleanAttribute(StyleFacade.SHOW_VERTEX_LABEL_ATTRIBUTE, showVertexLabelsCheckBox.isSelected());
			}
		});

		final JCheckBox showEdgesCheckBox = new JCheckBox("Показывать ребра", style.getBooleanAttribute(StyleFacade.EDGE_SHOW_ATTRIBUTE));
		systemPanel.add(showEdgesCheckBox);
		saveCallbacks.add(new ISaveCallback()
		{
			public void doSave()
			{
				new ShowEdgesAction(showEdgesCheckBox.isSelected()).execute();
			}
		});

		final JCheckBox _3DCheckBox = new JCheckBox("3D", StyleFacade.is3D(style));
		_3DCheckBox.setEnabled(false);
		systemPanel.add(_3DCheckBox);

		return systemPanel;
	}

}
